##########################################################################################################
## Payson (2021) When Cities Lobby Replication Code
## Chapter Four Appendix: Tables A4.1 - A4.2 and Figures A4.1
## Created with R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
##########################################################################################################


## Load packages
library(DataCombine); library(stargazer); library(lfe); 
library(tidyverse); 


## Functions for clean tex tables (need to define to compile tables)
getmean <- function(x) {
  round(mean(x$response), 2)}


## Set working directory
setwd()



##########################################################################################################
## Table A4.1 Geographic and Institutional Correlates of City Lobbying Across States
##########################################################################################################

dta <- read.csv("state_data.csv")

dta$termlim <- factor(dta$termlim)
dta$year <- factor(dta$year)
dta$limits <- factor(dta$limits)


m <- lm(lobby.pct ~ num.city + limits + city.density + frag + termlim + leg.prof + year, data = dta)

m1 <- lm(lobby.pct ~ num.city + limits + city.density + frag + termlim*leg.prof + 
           year, data = dta)

m2 <- lm(city.prop.ig ~ num.city + limits + city.density + frag + termlim*leg.prof, 
         data = subset(dta, year == "2007"))


stargazer(m, m1, m2, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05), 
          column.labels =  c("\\% Cities Lobbying", "Cities as \\% of All IGs (2007)"),
          keep = c("city.density", "frag", "termlim", "leg.prof", "num.city"),
          title = c("Geographic and Institutional Correlates of City Lobbying Across States. 
                    A higher percentage of cities lobby in states with both term limits and greater legislative 
                    professionalism. These two factors also predict cities comprising a larger 
                    proportion of all interest groups."), column.separate = c(2, 1),
          dep.var.caption = c(""), dep.var.labels.include = FALSE,
          covariate.labels = c("Total Cities", "Average City Population (Log)", "Cities Per Thousand Miles",
                               "Term Limits", "Legislative Professionalism", "Term Limits x Leg Prof"),
          add.lines = list(c("Year FEs", "Yes", "Yes", ""),
                           c("Observations", dim(m$model)[1], dim(m1$model)[1], dim(m2$model)[1]),
                           c("\\# States", 50, 50, 50),
                           c("Mean DV", round(mean(dta$lobby.pct), 1), 
                             round(mean(dta$lobby.pct), 1),
                             round(mean(dta$city.prop.ig, na.rm = T), 1))))



##########################################################################################################
## Table A4.2 Political Correlates of City Lobbying Across And Within States
##########################################################################################################

dta <- read.csv("state_data.csv")

dta$gov.partyR <- ifelse(dta$gov.party == "R", 1, 0)
dta$leg.controlR <- ifelse(dta$leg.control == "R", 1, 0)
dta$house.rep.pct <- dta$house.rep.prop*100
dta$year <- factor(dta$year)

m <- lm(lobby.pct ~ gov.partyR + house.rep.prop + divided.gov  + 
          house.np.diff + prop.state.transfers + city.density + year, data = dta)

m1 <- lm(lobby.pct ~ gov.partyR + house.rep.prop + divided.gov  + 
           house.np.diff + prop.state.transfers + city.density + state + year, data = dta)
summary(m1)

stargazer(m, m1, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05), 
          keep = c("city.density", "gov.partyR", "house.rep.prop", "divided.gov", "house.np.diff", 
                   "prop.state.transfers"),
          title = c("\\textbf{Political Correlates of City Lobbying Across States.} 
                    Across states, the presence of a Republican governor, a higher number of Republican house
                    representatives, partisan polarization, and divided government are all associated with
                    city lobbying. However, within states, these variables fail to explain variation. State transfers
                    as a share of municipal budgets predicts overall lobbying rates both across and within states."),
          dep.var.caption = "\\% Cities Lobbying", dep.var.labels.include = FALSE,
          covariate.labels = c("Republican Governor", "Proportion House Republicans", "Divided Government",
                               "House Polarization", "State Transfers As \\% of City Budgets", "Average City Population (Log)"),
          add.lines = list(c("Year FEs", "Yes", "Yes"),
                           c("State FEs", "", "Yes"),
                           c("Observations", dim(m$model)[1], dim(m1$model)[1]),
                           c("\\# States", 50, 50),
                           c("Mean DV", round(mean(dta$lobby.pct), 1), round(mean(dta$lobby.pct, na.rm = T), 1))))



##########################################################################################################
## Figure A4.1 Within-State Changes in City Lobbying, 2006–2015
##########################################################################################################

dta <- read.csv("state_data.csv")

dta <- dta %>%
  group_by(state) %>%
  mutate(lobby.mean = mean(lobby.pct, na.rm = T))

dta$lobby.demeaned <- dta$lobby.pct - dta$lobby.mean

(p <- ggplot(subset(dta, year > 2005), aes(x = year, y = lobby.pct)) +
    geom_line() +
    facet_wrap(~state) +
    scale_x_continuous(breaks = c(2006, 2008, 2010, 2012, 2014), labels = c("", "2008", "", "", "2014")) +
    labs(y = "% of Cities Lobbying", 
         x = "")  +
    theme_bw(base_family = "serif", base_size = 9) +
    theme(plot.background = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          strip.background = element_rect(fill = "white"),
          axis.title=element_text(size = 10)))


